<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

/**
 * 首页接口
 */
class Article extends Api
{
    protected $noNeedRight = ['*'];

    /**
     * 文章列表
     *
     */
    public function list()
    {
        // 页码
        $page = $this->request->get('page',1);
        // 每页数量
        $pageNum = $this->request->get('page_num',10);

        $list = model('Article')->where('status','=','normal')
            ->order('id', 'desc')
            ->limit(($page-1)*$pageNum,$pageNum)
            ->field('id,
                title,
                CONCAT("'.request()->domain().'",img_url) as img_url,
                content,
                FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%m:%s") as createtime,
                FROM_UNIXTIME(updatetime,"%Y-%m-%d %H:%m:%s") as updatetime')->select();
        $this->success('文章列表',$list);
    }

    /**
     * 文章详情
    */
    public function detail()
    {
        $id = $this->request->get('id');
        if(empty($id)){
            $this->error('请选择文章');
        }
        $detail = model('Article')->where([
            'id' => $id,
            'status'=>'normal'
        ])->field('id,
            title,
            CONCAT("'.request()->domain().'",img_url) as img_url,
            content,
            FROM_UNIXTIME(createtime,"%Y-%m-%d %H:%m:%s") as createtime,
            FROM_UNIXTIME(updatetime,"%Y-%m-%d %H:%m:%s") as updatetime'
        )->find();
        $this->success('文章列表',$detail);
    }
}
